<?php
/**
 * 限时折扣专区 
 *
 * @copyright  Copyright (c) 2007-2013 ShopNC Inc. (http://www.shopnc.net)
 * @license    http://www.shopnc.net
 * @link       http://www.shopnc.net
 * @since      File available since Release v1.1
 */
defined('InShopNC') or exit('Access Invalid!');
class goods_xianshiControl extends MobileControl{
	
    const XIANSHI_STATE_UNPUBLISHED = 1;
    const XIANSHI_STATE_PUBLISHED = 2;
    const XIANSHI_STATE_CANCEL = 3;
    const XIANSHI_STATE_INVADITATION = 4;
    const XIANSHI_STATE_END = 5;
    const XIANSHI_GOODS_STATE_NORMAL = 1;
    const XIANSHI_GOODS_STATE_CANCEL = 2;
	
	public function __construct(){
		parent::__construct();
		//检查商品促销功能是否开启
        if(intval($GLOBALS['setting_config']['promotion_allow']) !== 1) {
			mobileJump('商品促销功能暂未开启');
        }
	}
	
	public function indexOp(){
		//当前已发布的限时折扣活动列表
		$sql = "SELECT xianshi_id FROM #__p_xianshi WHERE state = ".self::XIANSHI_STATE_PUBLISHED;
		$act_list = Db::queryAll($sql);
		$id_arr = array();
		if(!empty($act_list) && is_array($act_list)){
			foreach($act_list as $v){
				$id_arr[] = $v['xianshi_id'];
			}
			$xianshi_id = implode(',',$id_arr);
		}else{
			mobileJump('暂无已发布的限时折扣活动');
		}
		
		//根据买家用户信息所在地区展示在相应地区展示的商品-市级-省级
		if(isset($_SESSION['is_login']) && (int)$_SESSION['is_login'] == 1){
			$member = Model('member')->infoMember(array('member_id'=>(int)$_SESSION['member_id']));
			$area_info = preg_split('/\t/', $member['member_areainfo'],-1,PREG_SPLIT_NO_EMPTY);
			if(count($area_info)==1){
				$province_id = (int)$member['member_provinceid'];
				$where = "AND FIND_IN_SET('$province_id',g.show_in_province_ids)";
			}elseif(count($area_info)==2 || count($area_info)==3){
				$city_id = (int)$member['member_cityid'];
				$where = "AND FIND_IN_SET('$city_id',g.show_in_city_ids)";
			}
		}
		
		//活动商品列表
		$pageSize = 10;
		$curPage = (int)$_GET['curpage'];
        $promotion_time = time ();
		$sql = "SELECT xg.*,g.store_id,px.end_time FROM #__p_xianshi_goods AS xg LEFT JOIN #__goods AS g ON xg.goods_id = g.goods_id LEFT JOIN #__p_xianshi AS px ON xg.xianshi_id = px.xianshi_id
		WHERE xg.xianshi_id IN ($xianshi_id) AND xg.state='1' AND g.goods_state = '0' AND g.goods_show = '1' AND ( px.start_time < '" . $promotion_time . "' ) AND ( px.end_time > '" . $promotion_time . "' ) AND g.xianshi_flag='1' $where";
		$rs = Db::PageQuery($sql,$curPage,$pageSize);
		$list = $rs->fetchAll();
		
		//分页
		$page	= new Page();
		$page->setEachNum($pageSize);
		$page->setNowPage($curPage);
		$page->setTotalNum($rs->pageRows());
		$page->setStyle('admin');
		
		$this->_assignMobileData($page);
		Tpl::output('list',$list);
		//echo '<pre>';print_r($list);die;
		Tpl::showpage('goods_xianshi');
	}
	
}